package cytj.setup.util;

/**
 * 字符工具类
 * Created by LTP on 2015/12/14 0014.
 */
public class CharacterTool {
    /**
     * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
     *
     * @param src0 byte
     * @param src1 byte
     * @return byte
     */
    public static byte uniteBytes(byte src0, byte src1) {
        byte b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        b0 = (byte) (b0 << 4);
        byte b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        return (byte) (b0 ^ b1);
    }

    /**
     * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF,
     * 0xD9}
     *
     * @param src String
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String src) {
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < tmp.length / 2; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

    /**
     * 异或校验工具
     *
     * @param bytes  要校验的字节数组
     * @param length 字节数组的前length个字节
     * @return 异或校验结果
     */
    public static String xor(byte[] bytes, int length) {
        byte b = 0;
        for (int i = 0; i < length; i++) {
            b ^= bytes[i];
        }
        String result = Integer.toHexString(b & 0xFF);
        if (result.length() == 1) {
            return "0" + result;
        }
        return result;
    }

    /**
     * 将字节数组转化为16进制字符串
     *
     * @param b byte[] 十进制字符数组
     * @return String 16进制字符串
     */
    public static String BytesToHexString(byte[] b, int len) {
        String ret = "";
        for (int i = 0; i < len; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

    /**
     * 将"i"转成bytes个字节（不足前补0）的i的16进制，如intToHexString(8400,3)="0020D0"
     *
     * @param str   "i"
     * @param bytes 字节数
     * @return 转换后的结果
     */
    public static String intToHexString(String str, int bytes) {
        int i = Integer.parseInt(str);
        String hexString = Integer.toHexString(i);
        String lenth0 = "";
        for (i = 0; i < 2 * bytes - hexString.length(); i++) {
            lenth0 += "0";
        }
        return lenth0 + hexString;
    }

    /**
     * 把IP地址格式型的字符串转成16进制字符串形式如parseIP("192.168.1.101")=c0a801;
     *
     * @param IPAddress IP地址
     * @return 转换后的结果
     */
    public static String parseIP(String IPAddress) {
        String[] str = IPAddress.split("\\.");
        return CharacterTool.intToHexString(str[0], 1) + CharacterTool.intToHexString(str[1], 1) + CharacterTool.intToHexString(str[2], 1) + CharacterTool.intToHexString(str[3], 1);
    }

    /**
     * 将协议数据分开，便于调试时查看 例：splitData("AAAA140001")="AA AA 14 00 01"
     *
     * @param data 接受到的协议数据
     * @return 分开后的数据
     */
    public static String splitData(String data) {
        String result = "";
        for (int i = 0; i < data.length(); i += 2) {
            result += data.substring(i, i + 2) + " ";
        }
        return result;
    }
}
